package algorithm.Base;

import java.util.*;

public class BinarySearch {
    public static void main(String[] args) {

        List<Integer> list = generateRandomIntegers(1, 10000000, 100000);
        int i = Collections.binarySearch(list, list.get(100));
        System.out.println("二分搜索的位置: "+i);
    }

    /**
     * 生成随机数
     * @param min
     * @param max
     * @param count
     * @return
     */
    public static List<Integer> generateRandomIntegers(int min, int max, int count) {
        Random random = new Random();
        // 用于存储不重复随机数的集合
        HashSet<Integer> uniqueRandomNumbers = new HashSet<>();
        for (int i = 0; i < count; i++) {
            int randomNumber = random.nextInt(max - min + 1) + min;
            // 确保生成的随机数不重复
            uniqueRandomNumbers.add(randomNumber);
        }
        // 将不重复随机数转换为 List 并打印
        List<Integer> randomList = new ArrayList<>(uniqueRandomNumbers);
        //打乱顺序
        Collections.shuffle(randomList);
        System.out.println("生成的随机整数范围在 " + min + " 到 " + max + " 之间，数量为 " + count + ":");
        for (int number : randomList) {
            System.out.print(number + " ");
        }
        System.out.println();
        return randomList;
    }
}
